using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Models.Trans.B2c;
using JlPay.Sdk.Models.Trans.Dto;
using JlPay.Sdk.Services.Trans.B2c;

namespace JlPay.Sdk.Examples.Trans.B2c
{
    /// <summary>
    /// 微支付示例
    /// </summary>
    public class MicroPayDemo
    {
        /// <summary>
        /// 构建微支付请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static MicroPayRequest BuildMicroPayRequest()
        {
            var request = new MicroPayRequest();
            
            // 设置基本参数
            request.MchId = "849584358120018";
            request.TermNo = "60131234";
            request.PayType = "WXPAY";
            request.OutTradeNo = GenerateOrderNo();
            request.PaymentValidTime = "300";
            request.Body = "测试商品";
            request.Attach = "test";
            request.TotalFee = "100";
            request.AuthCode = "134888883896712900";
            request.LimitPay = "";
            request.IsHirePurchase = "0";
            request.HirePurchaseNum = "0";
            request.HirePurchaseSellerPercent = "0";
            request.Remark = "测试";
            request.OpUserId = "001";
            request.OpShopId = "001";
            request.DeviceInfo = "N50007A8890";
            request.MchCreateIp = "123.12.12.123";
            request.Longitude = "116.078631";
            request.Latitude = "39.71274";
            request.SubAppid = "wxid_129752pfasf1671";
            request.GoodsTag = "sz001";
            
            // 设置商品详情
            var goodsData = new GoodsDataDto();
            goodsData.CostPrice = "100";
            goodsData.ReceiptId = "receipt_001";
            goodsData.GoodsDetail = new List<GoodsDetailDto>
            {
                new GoodsDetailDto
                {
                    GoodsId = "001",
                    GoodsName = "测试商品1",
                    Quantity = "1",
                    Price = "50"
                },
                new GoodsDetailDto
                {
                    GoodsId = "002", 
                    GoodsName = "测试商品2",
                    Quantity = "1",
                    Price = "50"
                }
            };
            request.GoodsData = goodsData;
            
            // 设置终端信息
            var terminalInfo = new TerminalInfoDto();
            terminalInfo.NetworkLicense = "123456789";
            terminalInfo.DeviceType = "01";
            terminalInfo.SerialNum = "TERM001";
            request.TerminalInfo = terminalInfo;
            
            return request;
        }

        /// <summary>
        /// 微支付示例
        /// </summary>
        public static async Task MicroPayDemoAsync()
        {
            Console.WriteLine("开始执行微支付示例");
            
            try
            {
                // 创建配置对象
                var config = DemoConfig.CreateConfigWithCrypto();
                var microPayService = new MicroPayService(config);
                
                // 创建微支付请求
                var request = BuildMicroPayRequest();
                
                // 发送请求
                var response = await microPayService.MicroPayAsync(request);
                
                // 处理响应结果
                response.HandleResponse("微支付", config);
            }
            catch (Exception ex)
            {
                var config = DemoConfig.CreateConfigWithCrypto();
                var response = new MicroPayResponse();
                response.HandleResponseWithException("微支付", config, ex);
            }
            
            Console.WriteLine("微支付示例执行完毕");
        }

        /// <summary>
        /// 生成订单号
        /// </summary>
        /// <returns>订单号</returns>
        private static string GenerateOrderNo()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(1000, 9999);
        }

        /// <summary>
        /// 主程序入口
        /// </summary>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 微支付示例 开始 =====");
            Console.WriteLine("=" + new string('=', 50));

            await MicroPayDemoAsync();

            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 微支付示例执行完毕 =====");
            Console.WriteLine("=" + new string('=', 50));
        }
    }
} 